package com.itheima.web;

import com.itheima.service.ReportService;
import com.itheima.support.DateData;
import com.itheima.support.Result;
import com.itheima.utils.ExportExcel;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;


@RestController
@RequestMapping("/report")
public class ReportController {

    @DubboReference
    private ReportService reportService;


    @GetMapping("/memberSexProportion")
    public Result memberSexProportion() {
        return new Result(reportService.memberSexProportion());
    }

    @GetMapping("/memberAgeProportion")
    public Result memberAgeProportion() {
        return new Result(reportService.memberAgeProportion());
    }



    @GetMapping("/businessExcel")
    public Result businessExcel(){
        return new Result(reportService.businessExcel());
    }

    @PostMapping("/exportExcel")
    public void download(@RequestBody Map map, HttpServletResponse response) throws IOException {

        response.reset();
        response.setHeader("Content-Disposition", "attachment");

        //response.setContentType("application/octet-stream;charset=UTF-8");
        ExportExcel.export(map,response.getOutputStream());
    }

    @PostMapping("/totalMemberCount")
    public Result totalMemberCount(@RequestBody DateData dateData) throws ParseException {
        return new Result(reportService.findMemberCountByDate(dateData));
    }

}
